<template>
    <page-view>
        <el-card>
            <template #header>
                <div class="font-bold">菜品列表</div>
            </template>
            <el-form :model="queryFormData" :inline="true">
                <el-form-item label="菜品名称">
                    <el-input placeholder="请输入菜品名称" v-model="queryFormData.food_name"></el-input>
                </el-form-item>
                <el-form-item label="菜品分类名称">
                    <el-input placeholder="请输入菜品分类名称" v-model="queryFormData.category_name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="queryData" :loading="isLoading" :loading-icon="Eleme">
                        <el-icon>
                            <Search />
                        </el-icon>
                        查询
                    </el-button>
                    <el-button type="success" @click="$router.push({ name: 'AddFoodInfo' })">
                        <el-icon>
                            <Aim />
                        </el-icon>
                        新增菜品
                    </el-button>
                </el-form-item>
            </el-form>
            <div v-loading="isLoading">
                <!-- 展示数据 -->
                <el-table :data="resultData.listData" border stripe max-height="550">
                    <el-table-column align="center" width="70" label="ID" prop="id"></el-table-column>
                    <el-table-column width="110" align="center" label="菜品名称" prop="food_name"></el-table-column>
                    <el-table-column width="110" align="center" label="菜品分类" prop="categoryInfo.category_name">
                    </el-table-column>
                    <el-table-column width="100" align="center" label="价格" prop="price"></el-table-column>
                    <el-table-column width="100" align="center" label="销售数量" prop="saleCount"></el-table-column>
                    <el-table-column label="菜品图片" width="120" align="center">
                        <template #default="{ row }">
                            <el-image :src="baseURL + row.food_img" alt="" class="w-[50px] h-[50px]" fit="cover"
                                :preview-src-list="[baseURL + row.food_img]" :preview-teleported="true" />
                        </template>
                    </el-table-column>
                    <el-table-column label="菜品简述" width="300">
                        <template #default="{ row }">
                            <el-popover placement="top-start" title="菜品简述" width="240" tigger="hover"
                                :content="row.food_desc">
                                <template #reference>
                                    <el-link underline>
                                        {{ textEllipsis(row.food_desc, 15) }}
                                    </el-link>
                                </template>
                            </el-popover>
                        </template>
                    </el-table-column>
                    <el-table-column label="菜品权重">
                        <template #default="{ row }">
                            <div class="flex flex-row justify-between items-center">
                                <el-slider :min="0" :max="100" :step="0.1" v-model="row.weight"
                                    @change="val => weightChange(val, row.id)" />
                                <span class="w-[40px] ml-[10px]">{{ row.weight }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="180" align="center">
                        <template #default="{ row }">
                            <el-button type="warning" size="small" @click="$router.push({name:'EditFoodInfo',params:{id:row.id}})">
                                <el-icon>
                                    <EditPen />
                                </el-icon>
                                编辑
                            </el-button>
                            <el-popconfirm title="你确定要删除吗" @confirm="delCurrentItem(row.id)">
                                <template #reference>
                                    <el-button type="danger" size="small">
                                        <el-icon>
                                            <Delete />
                                        </el-icon>
                                        删除
                                    </el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 页码 -->
                <div class="flex flex-row justify-between items-center mt-[10px]">
                    <div class="text-[14px]">当前第{{ queryFormData.pageIndex }}页，共{{ resultData.pageCount }}页，共{{
                            resultData.totalCount
                    }}条</div>
                    <el-pagination background layout="prev, pager, next" :total="resultData.totalCount"
                        @current-change="currentChange" />
                </div>
            </div>
        </el-card>
    </page-view>
</template>

<script setup>
import { Search, Eleme, Aim, EditPen, Delete } from "@element-plus/icons-vue"
import { reactive, onMounted, inject, ref, } from "vue"
import API from "../../utils/API";
import { textEllipsis } from "@/utils/StringUtils"
import { ElMessage } from "element-plus"
const queryFormData = reactive({
    food_name: "",
    category_name: "",
    pageIndex: 1
})

const baseURL = inject("baseURL");

const isLoading = ref(false);
const resultData = reactive({
    listData: [],
    pageCount: 0,
    pageStart: 0,
    totalCount: 0
})
// 查询操作
const queryData = () => {
    isLoading.value = true;
    API.foodInfo.getListByPage(queryFormData)
        .then(result => {
            // console.log(result);
            resultData.listData = result.listData;
            resultData.pageCount = result.pageCount;
            resultData.pageStart = result.pageStart;
            resultData.totalCount = result.totalCount;
        }).finally(() => {
            isLoading.value = false;

        })
}

const currentChange = (index) => {
    queryFormData.pageIndex = index;
    queryData();
}


const weightChange = (value, id) => {
    // console.log(value,id);
    API.foodInfo.setFoodInfoWeight({ id, weight: value })
}

// 删除按钮操作
const delCurrentItem = (id) => {
    // console.log(id);
    API.foodInfo.deleteById(id)
        .then(result => {
            ElMessage.success('删除成功')
            queryData();
        }).catch(error => {
            ElMessage.error('删除失败哦')
        })
}

onMounted(() => {
    queryData();
})

</script>


<style scoped lang="scss">

</style>